home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: Nested Structures in C - A Question
- Date: 28 Jan 1996 21:01:42 GMT
- Organization: Los Alamos National Laboratory
- Message-ID: <TANMOY.96Jan28140142@qcd.lanl.gov>
- References: <36400002@peg>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: tmccoy@peg.apc.org's message of 29 Jan 96 01:38 GMT+1000
-
- In article <36400002@peg> tmccoy@peg.apc.org writes:
- <snip>
- struct outer {
- int var1;
- int var2;
- int var3;
- };
-
- Apparently the compiler will NOT allocate any storage when
- I do this because, according to Kernighan and Ritchie (2nd
- Ed), "a structure declaration that is not followed by a
- list of variables reserves no storage; it merely describes
- a template or the shape of a structure" (Page 128).
-
- This is all well and good. And when I want to define a new
- structure *within* my old one, I should be able to do:
-
- struct outer {
- int var1;
- int var2;
- int var3;
- struct inner {
- int nested1;
- int nested2;
- };
- };
-
- Why do you think so? If you wanted to declare a new int inside your
- struct, would it have been sufficient to say `int;' instead of `int
- var4;'? The case with structs is exactly the same: `struct inner' is a
- type, like `int': not a field or variable name.
-
- The part in {} following the words struct inner actually specifies
- what the type struct inner is. (Someone else said that the declaration
- is valid because it declares the tag: that is incorrect. Inside a
- struct (e.g. struct outer), struct inner {...}; is a syntax error even
- though it declares the tag because a non-bitfield struct member
- declaration may not omit the declarator, i.e. the field name. In other
- words, only bitfields may be unnamed members.)
-
- <snip>
- Yet, C won't let me do this!! It insists that I define my
- nested structure as follows:
-
- struct outer {
- int var1;
- int var2;
- int var3;
- struct inner {
- int nested1;
- int nested2;
- } DUMMY;
- };
-
- and after doing
-
- struct outer instance;
-
- I must refer to the first member of my inner structure by
- doing:
-
- instance.DUMMY.nested1
-
- i.e. I must access the inner members via a variable (called
- "DUMMY") instead of being able to use my structure tag
- (i.e. "inner"). In fact, the *only way* I can access the
- inner members is by defining the DUMMY variable within the
- structure template of "outer".
-
- This is because the struct `tag' is a way of specifying the struct
- type. The . operator needs a field name, not a type name. Consider
- that the following is valid
-
- struct outer {
- struct inner { int nested1; int nested2; } DUMMY1,DUMMY2;
- } instance;
-
- Now what should instance.inner.nested1 (currently syntax error) mean?
- instance.DUMMY1.nested1 and instance.DUMMY2.nested2 are obvious.
-
- <snip>
- With a nested structure, as with a simple structure, I
- *should* be able to define a pure structure template in
- which no storage is allocated. Yet, in the definition I am
- forced to use above, strictly speaking the compiler should
- allocate storage for "DUMMY" even though the enclosing
- structure (i.e. "outer") is only a template.
-
- No, no storage is allocated. Just as when you say `int var1;' outside
- a struct it allocates storage, but not when it is inside a struct;
- similarly when you say `struct inner{ ...} DUMMY;' outside a struct,
- it allocates storage, not inside. Remember that storage is allocated
- only when you define a variable, not when you declare a type.
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-